package com.yx.system.service.impl;

import com.yx.modules.api.dao.*;
import com.yx.modules.api.entity.*;
import com.yx.system.authEntity.SysHanwuSection;
import com.yx.system.authEntity.SysHanwuText;
import com.yx.system.dao.*;
import com.yx.system.entity.*;
import com.yx.system.service.SysHanwuBookInfoService;
import com.yx.utils.HttpUtil;
import com.yx.utils.OrderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Service
public class SysHanwuBookInfoServiceImpl implements SysHanwuBookInfoService {
	@Autowired
	private SysHanwuBookInfoDao hanwuBookInfoDao;

	@Autowired
	private SysHanwuBookDao hanwuBookDao;

	@Autowired
	private TdCategoryMapper tdCategoryMapper;

	@Autowired
	private TdBookMapper tdBookMapper;

	@Autowired
	private TbVolumeDao tbVolumeDao;

	@Autowired
	private SysPriceDao sysPriceDao;

	@Autowired
	private TdTextMapper tdTextMapper;

	@Autowired
	private TdSectionMapper tdSectionMapper;

	@Autowired
	//图书评论
	private TbBookCommentDao tbBookCommentDao;

	@Autowired
	//图书评论点赞记录
	private TbCommentDianzanRecordDao tbCommentDianzanRecordDao;

	@Autowired
	//图书点赞记录
	private TbBookRecommendRecordDao tbBookRecommendRecordDao;

	@Autowired
	//图书点击记录
	private TbClickDao tbClickDao;

	@Autowired
	//图书打分记录
	private TbGradeRecordDao tbGradeRecordDao;

	@Autowired
	//图书打赏记录
	private TbRewardRecordDao tbRewardRecordDao;

	@Autowired
	//图书收藏记录
	private TdCollectionMapper tdCollectionMapper;

	@Autowired
	private TdBannerMapper tdBannerMapper;
	
	@Override
	public SysHanwuBookInfo queryObject(Integer id){
		return hanwuBookInfoDao.queryObject(id);
	}
	
	@Override
	public List<SysHanwuBookInfo> queryList(Map<String, Object> map){
		return hanwuBookInfoDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return hanwuBookInfoDao.queryTotal(map);
	}
	
	@Override
	public void save(SysHanwuBookInfo hanwuBookInfo){
		hanwuBookInfoDao.save(hanwuBookInfo);
	}
	
	@Override
	public void update(SysHanwuBookInfo hanwuBookInfo){
		hanwuBookInfoDao.update(hanwuBookInfo);
	}
	
	@Override
	public void delete(Integer id){
		hanwuBookInfoDao.delete(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteBatch(String[] ids){
		for (String arr:ids){
			//找到对应第三方图书详情数据
			SysHanwuBookInfo sysHanwuBookInfo = hanwuBookInfoDao.queryObject(arr);
			//找到对应的第三方图书
			SysHanwuBook sysHanwuBook = hanwuBookDao.queryObject(arr);
			//根据第三方图书找到图书对象
			TdBook tdBook = tdBookMapper.findByUUID(sysHanwuBookInfo.getUuid());
			if (tdBook != null){
				//删除图书评论
				List<TbBookComment> tbBookComments = tbBookCommentDao.findByBookid(tdBook.getId());
				if (tbBookComments.size()!=0){
					for (TbBookComment tbBookComment:tbBookComments){
						if (tbBookComment.getType()==0){
							//找到图书评论对应的点赞记录并删除
							List<TbCommentDianzanRecord> tbCommentDianzanRecords = tbCommentDianzanRecordDao.findByCommentid(tbBookComment.getId());
							if (tbCommentDianzanRecords.size()!=0){
								for (TbCommentDianzanRecord tbCommentDianzanRecord:tbCommentDianzanRecords){
									tbCommentDianzanRecordDao.delete(tbCommentDianzanRecord.getId());
								}
							}
						}
						tbBookCommentDao.delete(tbBookComment.getId());
					}
				}
				//删除图书点赞记录
				List<TbBookRecommendRecord> tbBookRecommendRecords = tbBookRecommendRecordDao.findByBookid(tdBook.getId());
				if (tbBookRecommendRecords.size()!=0){
					for (TbBookRecommendRecord tbBookRecommendRecord:tbBookRecommendRecords){
						tbBookRecommendRecordDao.delete(tbBookRecommendRecord.getId());
					}
				}
				//删除图书点击记录
				List<TbClick> tbClicks = tbClickDao.findByBookid(tdBook.getId());
				if (tbClicks.size()!=0){
					for (TbClick tbClick:tbClicks){
						tbClickDao.delete(tbClick.getId());
					}
				}
				//删除图书打分记录
				List<TbGradeRecord> tbGradeRecords = tbGradeRecordDao.findByBookid(tdBook.getId());
				if (tbGradeRecords.size()!=0){
					for (TbGradeRecord tbGradeRecord:tbGradeRecords){
						tbGradeRecordDao.delete(tbGradeRecord.getId());
					}
				}
				//删除图书打赏记录
				List<TbRewardRecord> tbRewardRecords = tbRewardRecordDao.findByBookid(tdBook.getId());
				if(tbRewardRecords.size()!=0){
					for (TbRewardRecord tbRewardRecord:tbRewardRecords){
						tbRewardRecordDao.delete(tbRewardRecord.getId());
					}
				}
				//删除图书收藏记录
				List<TdCollection> tdCollections = tdCollectionMapper.findByBookid(tdBook.getId());
				if (tdCollections.size()!=0){
					for (TdCollection tdCollection:tdCollections){
						tdCollectionMapper.deleteByPrimaryKey(tdCollection.getId());
					}
				}
				//删除banner图片记录
				List<TdBanner> tdBanners = tdBannerMapper.findByBookid(tdBook.getId());
				if (tdBanners.size()!=0){
					for (TdBanner tdBanner:tdBanners){
						tdBannerMapper.deleteByPrimaryKey(tdBanner.getId());
					}
				}
				//找到对应的卷
				List<TbVolume> tbVolumes = tbVolumeDao.findByBookId(tdBook.getId());
				if (tbVolumes.size()!=0){
					for (TbVolume tbVolume:tbVolumes){
						tbVolumeDao.delete(tbVolume.getId());
					}
				}
				List<TdSection> tdSectionList = tdSectionMapper.findByBookid(tdBook.getId());
				//循环章节找到内容一起删除
				if (tdSectionList.size()!=0){
					for (TdSection tdSection1 : tdSectionList) {
						//找到对应的TEXT
						TdText tdText1 = tdTextMapper.findByUid(tdSection1.getUuid());
						if (tdText1!=null){
							tdTextMapper.deleteByPrimaryKey(tdText1.getId());
						}
						//开始删除
						tdSectionMapper.deleteByPrimaryKey(tdSection1.getId());
					}
				}
				//删除图书
				tdBookMapper.deleteByPrimaryKey(tdBook.getId());
			}

			//删除第三方数据
			if (sysHanwuBookInfo!=null){
				hanwuBookInfoDao.delete(sysHanwuBookInfo.getArticleid());
			}
			if (sysHanwuBook!=null){
				hanwuBookDao.delete(sysHanwuBook.getArticleid());
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void put(SysHanwuBookInfo hanwuBookInfo, SysUser sysUser) {
		//取出一级分类id,并查询
		Integer categoryid = hanwuBookInfo.getCid();
		TdCategory tdCategory = tdCategoryMapper.selectByPrimaryKey(categoryid);
		//取出章节收费
		List<SysPrice> sysPrices = sysPriceDao.findAll();
        /*if (sysPrices.size() > 1 || sysPrices.size() == 0) {
            return R.error("请先设置章节收费，并只能存在一条");
        }*/
		SysPrice sysPrice = sysPrices.get(0);
		//初始化字数计算正则
		String regex = "[\\u4e00-\\u9fa5]|[\\，\\。\\.]";
		String regex2 = "[宋体]|[黑体]|[微软雅黑]|[楷体]";
		try {
			if(hanwuBookInfo.getStatus()==0) {
				//新建一个tdbook
				TdBook tdBook = new TdBook();
				//生成一个随机唯一标识
				String arr = OrderUtil.getNumberForPK();
				//注入值
				tdBook.setCreatedate(new Date());
				tdBook.setUpdatedate(new Date());
				tdBook.setBookname(hanwuBookInfo.getArticlename());
				tdBook.setPicurl(hanwuBookInfo.getCover());
				tdBook.setBooknum((int) Math.random());
				tdBook.setCategoryid(categoryid);
				tdBook.setCategoryname(tdCategory.getCategoryyname());
				tdBook.setBookno(arr);
				tdBook.setClassificationid(0);
				tdBook.setClassificationname("");
				tdBook.setSource("寒武纪年");
				tdBook.setOrigin(3);
				tdBook.setBooktype(1);
				if (hanwuBookInfo.getIsvip().equals("1")) {
					tdBook.setIspay(true);
				} else {
					tdBook.setIspay(false);
				}
				if (hanwuBookInfo.getFullflag().equals("1")) {
					tdBook.setIsserialize(false);
				} else {
					tdBook.setIsserialize(true);
				}
				tdBook.setAuthorCode("0");
				tdBook.setAuthorName(hanwuBookInfo.getAuthor());
				tdBook.setIsxianyan(false);
				tdBook.setIsxianjie(false);
				tdBook.setIswuxia(false);
				tdBook.setIssanjie(false);
				tdBook.setIsremen(false);
				tdBook.setIsqihuan(false);
				tdBook.setIsjingxuan(false);
				tdBook.setIsgongting(false);
				tdBook.setIsdelete(false);
				tdBook.setIntroduction(hanwuBookInfo.getIntro());
				tdBook.setChecknum(0);
				tdBook.setSell(0);
				tdBook.setGrade(Double.valueOf(0));
				tdBook.setGradeSum(Double.valueOf(0));
				tdBook.setGradeNumber(0);
				tdBook.setWordType(0);
				tdBook.setLikeCount(0);
				tdBook.setCollectionCount(0);
				tdBook.setRewardCount(0);
				tdBook.setRewardMoney(0);
				tdBook.setCommentCount(0);
				tdBook.setCommentBoyCount(0);
				tdBook.setCommentGirlCount(0);
				tdBook.setFontCount(0);
				tdBook.setSectionNumber(0);
				tdBook.setBooktype(1);
				tdBook.setSubscibeMoney(0);
				tdBook.setGuarantee(Double.valueOf(0));
				//保存图书
				tdBookMapper.insert(tdBook);
			/*==========================获取第三方的章节接口=============================*/
				String sectionSign = HttpUtil.encryption("aid=" + hanwuBookInfo.getArticleid() + "&sid=20&key=0cPfpLrPEnt7MiCiNAh48asyjbDP2v8w");
				String sectionTemp = HttpUtil.sendGet("http://www.hanwujinian.com/apis/sid/articlechapter.php?aid=" + hanwuBookInfo.getArticleid() + "&sid=20&sign=" + sectionSign);
				//获取寒武图书的章节集合
				List<SysHanwuSection> sysHanwuSections = HttpUtil.jsonToList(sectionTemp, SysHanwuSection.class);
				//初始化一个卷
				TbVolume tbVolume = new TbVolume();
				tbVolume.setId(Integer.valueOf(OrderUtil.getNumberForInt()));
				tbVolume.setVolumeName("初始卷");
				tbVolume.setBookId(tdBook.getId());
				tbVolume.setBookName(tdBook.getBookname());
				tbVolume.setCreateDate(new Date());
				tbVolume.setUpdateDate(new Date());
				tbVolumeDao.save(tbVolume);
				//循环获取的章节列表
				for (SysHanwuSection sysHanwuSection : sysHanwuSections) {
					//第一个必须为卷
					if (sysHanwuSection.getChaptertype().equals("1")) {
						//更新卷的属性
						tbVolume = new TbVolume();
						tbVolume.setId(Integer.valueOf(OrderUtil.getNumberForInt()));
						tbVolume.setVolumeName(sysHanwuSection.getChaptername());
						tbVolume.setBookId(tdBook.getId());
						tbVolume.setBookName(tdBook.getBookname());
						tbVolume.setCreateDate(new Date());
						tbVolume.setUpdateDate(new Date());
						//保存章节
						tbVolumeDao.save(tbVolume);
						//如果是章节，就开始创建章节
					} else {
						//创建一个新的章节
						TdSection tdSection = new TdSection();
						//注入属性
						tdSection.setUuid(UUID.randomUUID().toString());
						tdSection.setSectionStatus(0);
						tdSection.setSectionname(sysHanwuSection.getChaptername());
						tdSection.setBookid(tdBook.getId());
						tdSection.setIsdelete(false);
						tdSection.setSectionno((int) Math.random());
						tdSection.setCreatedate(new Date());
						tdSection.setCreateuser(sysUser.getUsername());
						tdSection.setUpdatedate(new Date());
						tdSection.setUpdateuser(sysUser.getUsername());
						tdSection.setPicurl(tdBook.getPicurl());
						tdSection.setVolumeId(tbVolume.getId());
						tdSection.setVolumeName(tbVolume.getVolumeName());
						tdSection.setFontsize(Integer.valueOf(sysHanwuSection.getWords()));
						if (sysHanwuSection.getIsvip().equals("1")) {
							tdSection.setSectionType(1);
							BigDecimal bg = new BigDecimal(tdSection.getFontsize()/1000);
							Double fontSize = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
							tdSection.setTotalfee((int) (sysPrice.getPrice()*fontSize));
						} else {
							tdSection.setSectionType(0);
							tdSection.setTotalfee(0);
						}
						//保存章节
						tdSectionMapper.insert(tdSection);
						//图书设置最新章节并保存
						tdBook.setNewsectionid(tdSection.getId());
						tdBook.setNewsection(tdSection.getSectionname());
						tdBook.setSectionNumber(tdBook.getSectionNumber() + 1);
						tdBookMapper.updateByPrimaryKey(tdBook);
					/*==========================章节内容=============================*/
						String textSign = HttpUtil.encryption("aid=" + hanwuBookInfo.getArticleid() + "&cid=" + sysHanwuSection.getChapterid() + "&sid=20&key=0cPfpLrPEnt7MiCiNAh48asyjbDP2v8w");
						String textTemp = HttpUtil.sendGet("http://www.hanwujinian.com/apis/sid/chaptercontent.php?aid=" + hanwuBookInfo.getArticleid() + "&cid=" + sysHanwuSection.getChapterid() + "&sid=20&sign=" + textSign);
						SysHanwuText sysHanwuText = HttpUtil.tobean(textTemp, SysHanwuText.class);
						//新建text对象
						TdText tdText = new TdText();
						tdText.setId(UUID.randomUUID().toString());
						tdText.setUid(tdSection.getUuid());
						tdText.setBookname(tdBook.getBookname());
						tdText.setSection(tdSection.getSectionname());
						tdText.setAuthor(tdBook.getAuthorName());
						tdText.setCreatetime(new Date());
						tdText.setIsdelete(0);
						tdText.setText(sysHanwuText.getContent());
						//保存text
						tdTextMapper.insert(tdText);
					}
				}
			}else {
				//首先根据uuid查询图书
				TdBook tdBook = tdBookMapper.findByUUID(hanwuBookInfo.getUuid());
				//如果为空就是新添加
				if (tdBook==null){
					//新建一个tdbook
					tdBook = new TdBook();
					//生成一个随机唯一标识
					String arr = OrderUtil.getNumberForPK();
					//注入值
					tdBook.setCreatedate(new Date());
					tdBook.setUpdatedate(new Date());
					tdBook.setBookname(hanwuBookInfo.getArticlename());
					tdBook.setPicurl(hanwuBookInfo.getCover());
					tdBook.setBooknum((int) Math.random());
					tdBook.setCategoryid(categoryid);
					tdBook.setCategoryname(tdCategory.getCategoryyname());
					tdBook.setBookno(arr);
					tdBook.setClassificationid(0);
					tdBook.setClassificationname("");
					tdBook.setSource("寒武纪年");
					tdBook.setOrigin(3);
					tdBook.setBooktype(1);
					if (hanwuBookInfo.getIsvip().equals("1")) {
						tdBook.setIspay(true);
					} else {
						tdBook.setIspay(false);
					}
					if (hanwuBookInfo.getFullflag().equals("1")) {
						tdBook.setIsserialize(false);
					} else {
						tdBook.setIsserialize(true);
					}
					tdBook.setAuthorCode("0");
					tdBook.setAuthorName(hanwuBookInfo.getAuthor());
					tdBook.setIsxianyan(false);
					tdBook.setIsxianjie(false);
					tdBook.setIswuxia(false);
					tdBook.setIssanjie(false);
					tdBook.setIsremen(false);
					tdBook.setIsqihuan(false);
					tdBook.setIsjingxuan(false);
					tdBook.setIsgongting(false);
					tdBook.setIsdelete(false);
					tdBook.setIntroduction(hanwuBookInfo.getIntro());
					tdBook.setChecknum(0);
					tdBook.setSell(0);
					tdBook.setGrade(Double.valueOf(0));
					tdBook.setGradeSum(Double.valueOf(0));
					tdBook.setGradeNumber(0);
					tdBook.setWordType(0);
					tdBook.setLikeCount(0);
					tdBook.setCollectionCount(0);
					tdBook.setRewardCount(0);
					tdBook.setRewardMoney(0);
					tdBook.setCommentCount(0);
					tdBook.setCommentBoyCount(0);
					tdBook.setCommentGirlCount(0);
					tdBook.setFontCount(0);
					tdBook.setSectionNumber(0);
					tdBook.setBooktype(1);
					tdBook.setSubscibeMoney(0);
					tdBook.setGuarantee(Double.valueOf(0));
					//保存图书
					tdBookMapper.insert(tdBook);
			/*==========================获取第三方的章节接口=============================*/
					String sectionSign = HttpUtil.encryption("aid="+hanwuBookInfo.getArticleid()+"&sid=20&key=0cPfpLrPEnt7MiCiNAh48asyjbDP2v8w");
					String sectionTemp = HttpUtil.sendGet("http://www.hanwujinian.com/apis/sid/articlechapter.php?aid="+hanwuBookInfo.getArticleid()+"&sid=20&sign="+sectionSign);
					//获取寒武图书的章节集合
					List<SysHanwuSection> sysHanwuSections = HttpUtil.jsonToList(sectionTemp,SysHanwuSection.class);
					//初始化一个卷
					TbVolume tbVolume = new TbVolume();
					tbVolume.setId(Integer.valueOf(OrderUtil.getNumberForInt()));
					tbVolume.setVolumeName("初始卷");
					tbVolume.setBookId(tdBook.getId());
					tbVolume.setBookName(tdBook.getBookname());
					tbVolume.setCreateDate(new Date());
					tbVolume.setUpdateDate(new Date());
					tbVolumeDao.save(tbVolume);
					//循环获取的章节列表
					for (SysHanwuSection sysHanwuSection:sysHanwuSections){
						//第一个必须为卷
						if (sysHanwuSection.getChaptertype().equals("1")){
							//更新卷的属性
							tbVolume = new TbVolume();
							tbVolume.setId(Integer.valueOf(OrderUtil.getNumberForInt()));
							tbVolume.setVolumeName(sysHanwuSection.getChaptername());
							tbVolume.setBookId(tdBook.getId());
							tbVolume.setBookName(tdBook.getBookname());
							tbVolume.setCreateDate(new Date());
							tbVolume.setUpdateDate(new Date());
							//保存章节
							tbVolumeDao.save(tbVolume);
							//如果是章节，就开始创建章节
						}else {
							//创建一个新的章节
							TdSection tdSection = new TdSection();
							//注入属性
							tdSection.setUuid(UUID.randomUUID().toString());
							tdSection.setSectionStatus(0);
							tdSection.setSectionname(sysHanwuSection.getChaptername());
							tdSection.setBookid(tdBook.getId());
							tdSection.setIsdelete(false);
							tdSection.setSectionno((int) Math.random());
							tdSection.setCreatedate(new Date());
							tdSection.setCreateuser(sysUser.getUsername());
							tdSection.setUpdatedate(new Date());
							tdSection.setUpdateuser(sysUser.getUsername());
							tdSection.setPicurl(tdBook.getPicurl());
							tdSection.setVolumeId(tbVolume.getId());
							tdSection.setVolumeName(tbVolume.getVolumeName());
							tdSection.setFontsize(Integer.valueOf(sysHanwuSection.getWords()));
							if (sysHanwuSection.getIsvip().equals("1")){
								tdSection.setSectionType(1);
								BigDecimal bg = new BigDecimal(tdSection.getFontsize()/1000);
								Double fontSize = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
								tdSection.setTotalfee((int) (sysPrice.getPrice()*fontSize));
							}else {
								tdSection.setSectionType(0);
								tdSection.setTotalfee(0);
							}
							//保存章节
							tdSectionMapper.insert(tdSection);
							//图书设置最新章节并保存
							tdBook.setNewsectionid(tdSection.getId());
							tdBook.setNewsection(tdSection.getSectionname());
							tdBook.setSectionNumber(tdBook.getSectionNumber()+1);
							tdBookMapper.updateByPrimaryKey(tdBook);
					/*==========================章节内容=============================*/
							String textSign = HttpUtil.encryption("aid="+hanwuBookInfo.getArticleid()+"&cid="+sysHanwuSection.getChapterid()+"&sid=20&key=0cPfpLrPEnt7MiCiNAh48asyjbDP2v8w");
							String textTemp = HttpUtil.sendGet("http://www.hanwujinian.com/apis/sid/chaptercontent.php?aid="+hanwuBookInfo.getArticleid()+"&cid="+sysHanwuSection.getChapterid()+"&sid=20&sign="+textSign);
							SysHanwuText sysHanwuText = HttpUtil.tobean(textTemp,SysHanwuText.class);
							//新建text对象
							TdText tdText = new TdText();
							tdText.setId(UUID.randomUUID().toString());
							tdText.setUid(tdSection.getUuid());
							tdText.setBookname(tdBook.getBookname());
							tdText.setSection(tdSection.getSectionname());
							tdText.setAuthor(tdBook.getAuthorName());
							tdText.setCreatetime(new Date());
							tdText.setIsdelete(0);
							tdText.setText(sysHanwuText.getContent());
							//保存text
							tdTextMapper.insert(tdText);
						}
					}
					//改变寒武图书详情属性
					hanwuBookInfo.setUpdateDate(new Date());
					hanwuBookInfo.setStatus(1);
					hanwuBookInfo.setUuid(arr);
					hanwuBookInfoDao.update(hanwuBookInfo);
					//如果不为空就是再导入
				}else {
					//更新属性并更新
					tdBook.setCategoryid(categoryid);
					tdBook.setCategoryname(tdCategory.getCategoryyname());
					tdBook.setIntroduction(hanwuBookInfo.getIntro());
					tdBook.setBookname(hanwuBookInfo.getArticlename());
					tdBook.setPicurl(hanwuBookInfo.getCover());
					tdBook.setAuthorName(hanwuBookInfo.getAuthor());
					tdBook.setSectionNumber(0);
					tdBook.setBooktype(1);
					tdBook.setOrigin(3);
					tdBook.setSource("寒武纪年");
					if (hanwuBookInfo.getIsvip().equals("1")) {
						tdBook.setIspay(true);
					} else {
						tdBook.setIspay(false);
					}
					if (hanwuBookInfo.getFullflag().equals("1")) {
						tdBook.setIsserialize(false);
					} else {
						tdBook.setIsserialize(true);
					}
					tdBookMapper.updateByPrimaryKey(tdBook);
			/*==========================删除图书下的卷、章节、内容============================*/
					List<TbVolume> tbVolumes = tbVolumeDao.findByBookId(tdBook.getId());
					if (tbVolumes.size()!=0){
						for (TbVolume tbVolume:tbVolumes){
							tbVolumeDao.delete(tbVolume.getId());
						}
					}
					List<TdSection> tdSectionList = tdSectionMapper.findByBookid(tdBook.getId());
					//循环章节找到内容一起删除
					if (tdSectionList.size()!=0){
						for (TdSection tdSection1 : tdSectionList) {
							//找到对应的TEXT
							TdText tdText1 = tdTextMapper.findByUid(tdSection1.getUuid());
							//开始删除
							if (tdText1!=null){
								tdTextMapper.deleteByPrimaryKey(tdText1.getId());
							}
							tdSectionMapper.deleteByPrimaryKey(tdSection1.getId());
						}
					}
			/*=================================重新添加所有的卷、章节、内容==========================================*/
					String sectionSign = HttpUtil.encryption("aid="+hanwuBookInfo.getArticleid()+"&sid=20&key=0cPfpLrPEnt7MiCiNAh48asyjbDP2v8w");
					String sectionTemp = HttpUtil.sendGet("http://www.hanwujinian.com/apis/sid/articlechapter.php?aid="+hanwuBookInfo.getArticleid()+"&sid=20&sign="+sectionSign);
					//获取寒武图书的章节集合
					List<SysHanwuSection> sysHanwuSections = HttpUtil.jsonToList(sectionTemp,SysHanwuSection.class);
					//初始化一个卷
					TbVolume tbVolume = new TbVolume();
					tbVolume.setId(Integer.valueOf(OrderUtil.getNumberForInt()));
					tbVolume.setVolumeName("初始卷");
					tbVolume.setBookId(tdBook.getId());
					tbVolume.setBookName(tdBook.getBookname());
					tbVolume.setCreateDate(new Date());
					tbVolume.setUpdateDate(new Date());
					tbVolumeDao.save(tbVolume);
					//循环获取的章节列表
					for (SysHanwuSection sysHanwuSection:sysHanwuSections){
						//第一个必须为卷
						if (sysHanwuSection.getChaptertype().equals("1")){
							//更新卷的属性
							tbVolume = new TbVolume();
							tbVolume.setId(Integer.valueOf(OrderUtil.getNumberForInt()));
							tbVolume.setVolumeName(sysHanwuSection.getChaptername());
							tbVolume.setBookId(tdBook.getId());
							tbVolume.setBookName(tdBook.getBookname());
							tbVolume.setCreateDate(new Date());
							tbVolume.setUpdateDate(new Date());
							//保存章节
							tbVolumeDao.save(tbVolume);
							//如果是章节，就开始创建章节
						}else {
							//创建一个新的章节
							TdSection tdSection = new TdSection();
							//注入属性
							tdSection.setUuid(UUID.randomUUID().toString());
							tdSection.setSectionStatus(0);
							tdSection.setSectionname(sysHanwuSection.getChaptername());
							tdSection.setBookid(tdBook.getId());
							tdSection.setIsdelete(false);
							tdSection.setSectionno((int) Math.random());
							tdSection.setCreatedate(new Date());
							tdSection.setCreateuser(sysUser.getUsername());
							tdSection.setUpdatedate(new Date());
							tdSection.setUpdateuser(sysUser.getUsername());
							tdSection.setPicurl(tdBook.getPicurl());
							tdSection.setVolumeId(tbVolume.getId());
							tdSection.setVolumeName(tbVolume.getVolumeName());
							tdSection.setFontsize(Integer.valueOf(sysHanwuSection.getWords()));
							if (sysHanwuSection.getIsvip().equals("1")){
								tdSection.setSectionType(1);
								BigDecimal bg = new BigDecimal(tdSection.getFontsize()/1000);
								Double fontSize = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
								tdSection.setTotalfee((int) (sysPrice.getPrice()*fontSize));
							}else {
								tdSection.setSectionType(0);
								tdSection.setTotalfee(0);
							}
							//保存章节
							tdSectionMapper.insert(tdSection);
							//图书设置最新章节并保存
							tdBook.setNewsectionid(tdSection.getId());
							tdBook.setNewsection(tdSection.getSectionname());
							tdBook.setSectionNumber(tdBook.getSectionNumber()+1);
							tdBookMapper.updateByPrimaryKey(tdBook);
					/*==========================章节内容=============================*/
							String textSign = HttpUtil.encryption("aid="+hanwuBookInfo.getArticleid()+"&cid="+sysHanwuSection.getChapterid()+"&sid=20&key=0cPfpLrPEnt7MiCiNAh48asyjbDP2v8w");
							String textTemp = HttpUtil.sendGet("http://www.hanwujinian.com/apis/sid/chaptercontent.php?aid="+hanwuBookInfo.getArticleid()+"&cid="+sysHanwuSection.getChapterid()+"&sid=20&sign="+textSign);
							SysHanwuText sysHanwuText = HttpUtil.tobean(textTemp,SysHanwuText.class);
							//新建text对象
							TdText tdText = new TdText();
							tdText.setId(UUID.randomUUID().toString());
							tdText.setUid(tdSection.getUuid());
							tdText.setBookname(tdBook.getBookname());
							tdText.setSection(tdSection.getSectionname());
							tdText.setAuthor(tdBook.getAuthorName());
							tdText.setCreatetime(new Date());
							tdText.setIsdelete(0);
							tdText.setText(sysHanwuText.getContent());
							//保存text
							tdTextMapper.insert(tdText);
						}
					}
					//改变寒武图书详情属性
					hanwuBookInfo.setUpdateDate(new Date());
					hanwuBookInfo.setStatus(1);
					hanwuBookInfoDao.update(hanwuBookInfo);
				}
			}
		}catch (Exception e){
			e.printStackTrace();
			hanwuBookInfo.setStatus(3);
			hanwuBookInfoDao.update(hanwuBookInfo);
		}

	}

}
